package controller;

import com.qq.connect.QQConnectException;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.oauth.Oauth;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.QqTheThirdPartyService;
import util.ResponseBean;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class QqTheThirdParty {

    @Resource
    private ResponseBean responseBean;

    @Resource
    private QqTheThirdPartyService qqTheThirdPartyService;

    //返回扫描的URL给前台
    @GetMapping("/getqq")
    @ResponseBody
    public ResponseBean QqTheThirdPartyLogin(HttpServletRequest request) throws QQConnectException {

        String url = new Oauth().getAuthorizeURL(request);

        responseBean.setAll(ResponseBean.HTTP_CODE_OK, "Log In Yes", url);

        return responseBean;
    }

    //QQ第三方登录成功回调方法个人
    @RequestMapping("qq")
    @ResponseBody
    public ResponseBean QqTheThirdPartyCallback(HttpServletRequest request) throws QQConnectException {

        AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);

        if (accessTokenObj.getAccessToken().equals("")) {
            //网站被CSRF攻击了或者用户取消了授权
            //后续补上
            return responseBean;
        } else {

            responseBean=qqTheThirdPartyService.QqTheThirdPartyCallback(accessTokenObj);

        }

        return responseBean;
    }


}
